package com.onlineMIS.ORM.DAO.headQ.SQLServer;


import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAOMS;
import com.onlineMIS.ORM.entity.headQ.SQLServer.PriceMS;
import com.onlineMIS.ORM.entity.headQ.SQLServer.ProductsMS;
import com.onlineMIS.ORM.entity.headQ.SQLServer.buymanagebilldrf;
import com.onlineMIS.ORM.entity.headQ.SQLServer.storemanagebilldrf;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Brand;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Product;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.ORM.entity.headQ.supplier.purchase.PurchaseOrderProduct;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.loggerLocal;

@Repository
public class StoremanagebilldrfDAOImpl extends BaseDAOMS<storemanagebilldrf> {

	public double saveBillDtfDetails(PurchaseOrderProduct orderProduct, int billid, int orderId,
			Map<String, ProductBarcode> productMap, Map<String, ProductsMS> productMSMap) {

		int quantity = orderProduct.getQuantity();
		String barcode = orderProduct.getPb().getBarcode();

		ProductsMS productsMS = productMSMap.get(barcode);
		Product product = productMap.get(barcode).getProduct();
		int productMSID = productsMS.getProductID();
		String comment = product.getUnit();

//new release logic	
		PriceMS priceMS = productsMS.getPrice();
		double retailPrice = priceMS.getRetailprice();
		double recPrice = priceMS.getRecprice();

		double discountPrice = Common_util.roundDouble(orderProduct.getRecCost(),2);

		double discount_price = discountPrice;
		double tax_price = discountPrice;
		double new_price = discountPrice;

		double totalMoney = Common_util.roundDouble(quantity * discount_price, 2);
		double tax_total = Common_util.roundDouble(quantity * tax_price, 2);

		// 验证是否是需要尺码的货品
		int brandId = product.getBrand().getBrand_ID();
		int status = 0;
		if (Brand.SIZE_BRAND.contains(brandId)) {
			status = 1;
		}


		storemanagebilldrf buymanagebilldrf = new storemanagebilldrf();
		buymanagebilldrf.setBill_id(billid);
		buymanagebilldrf.setP_id(productMSID);
		buymanagebilldrf.setBatchno("");
		buymanagebilldrf.setQuantity(quantity);
		buymanagebilldrf.setCostprice(recPrice);
		buymanagebilldrf.setPrice(discount_price);
		buymanagebilldrf.setTotalmoney(totalMoney);
		buymanagebilldrf.setTaxprice(tax_price);
		buymanagebilldrf.setTaxtotal(tax_total);
		buymanagebilldrf.setTaxmoney(0);
		buymanagebilldrf.setRetailprice(retailPrice);
		buymanagebilldrf.setRetailmoney(retailPrice * quantity);
		buymanagebilldrf.setMakedate(new Date(0, 1, 1));
		buymanagebilldrf.setValiddate(new Date(0, 1, 1));
		buymanagebilldrf.setQualitystatus("合格");
		buymanagebilldrf.setPrice_id(0);
		buymanagebilldrf.setSs_id(2);
		buymanagebilldrf.setSd_id(0);
		buymanagebilldrf.setLocation_id(0);
		buymanagebilldrf.setSupplier_id(0);
		buymanagebilldrf.setCommissionflag(0);
		buymanagebilldrf.setComment(comment);
		buymanagebilldrf.setUnitid(productsMS.getUnit_id());
		buymanagebilldrf.setTaxrate(0);
		buymanagebilldrf.setOrder_id(0);
		buymanagebilldrf.setIotag(0);
		buymanagebilldrf.setInvoiceTotal(0);
		buymanagebilldrf.setThqty(quantity);
		buymanagebilldrf.setNewprice(new_price);
		buymanagebilldrf.setOrgbillid(0);
		buymanagebilldrf.setSizeID(0);
		buymanagebilldrf.setColorID(0);
		buymanagebilldrf.setStatus(status);
		buymanagebilldrf.setInvoice(0);
		buymanagebilldrf.setInvoiceno("");
		buymanagebilldrf.setJSPRICE(0);
		buymanagebilldrf.setGroupid(0);
		buymanagebilldrf.setDraftRowID(0);
		buymanagebilldrf.setAOID(0);
		buymanagebilldrf.setBusinessType(0);
		buymanagebilldrf.setRowTag(0);
		buymanagebilldrf.setRowGuid(UUID.randomUUID().toString());
		buymanagebilldrf.setBillguid(UUID.randomUUID().toString());
		buymanagebilldrf.setPriceType("");
		buymanagebilldrf.setTotal(totalMoney);

		this.save(buymanagebilldrf, false);
		
		loggerLocal.info("SupplierPurchaseService.saveToJinSuan(): storemanagebilldrf : order_id:" + orderId
				+ ", Jinsuan id:" + billid + "," + barcode + "," + productMSID + "," + quantity);

		return totalMoney;
	}


}
